From e8b30f30094937e8628e9f5cb732904fa07d45c8 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 2 Jun 2017 06:59:16 -0700 Subject: [PATCH] Use `Rc::make_mut` in `Dependency` Follow the same pattern with `Summary` --- src/cargo/core/dependency.rs | 42 ++++++++++++++++++----------- src/cargo/sources/registry/index.rs | 14 +++++----- src/cargo/util/toml.rs | 24 ++++++++--------- tests/resolve.rs | 2 +- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index 3b75934eb..4b363e6a7 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -180,49 +180,49 @@ this warning. self.platform.as_ref() } - pub fn set_kind(mut self, kind: Kind) -> DependencyInner { + pub fn set_kind(&mut self, kind: Kind) -> &mut DependencyInner { self.kind = kind; self } /// Sets the list of features requested for the package. - pub fn set_features(mut self, features: Vec) -> DependencyInner { + pub fn set_features(&mut self, features: Vec) -> &mut DependencyInner { self.features = features; self } /// Sets whether the dependency requests default features of the package. - pub fn set_default_features(mut self, default_features: bool) -> DependencyInner { + pub fn set_default_features(&mut self, default_features: bool) -> &mut DependencyInner { self.default_features = default_features; self } /// Sets whether the dependency is optional. - pub fn set_optional(mut self, optional: bool) -> DependencyInner { + pub fn set_optional(&mut self, optional: bool) -> &mut DependencyInner { self.optional = optional; self } /// Set the source id for this dependency - pub fn set_source_id(mut self, id: SourceId) -> DependencyInner { + pub fn set_source_id(&mut self, id: SourceId) -> &mut DependencyInner { self.source_id = id; self } /// Set the version requirement for this dependency - pub fn set_version_req(mut self, req: VersionReq) -> DependencyInner { + pub fn set_version_req(&mut self, req: VersionReq) -> &mut DependencyInner { self.req = req; self } - pub fn set_platform(mut self, platform: Option) - -> DependencyInner { + pub fn set_platform(&mut self, platform: Option) + -> &mut DependencyInner { self.platform = platform; self } /// Lock this dependency to depending on the specified package id - pub fn lock_to(self, id: &PackageId) -> DependencyInner { + pub fn lock_to(&mut self, id: &PackageId) -> &mut DependencyInner { assert_eq!(self.source_id, *id.source_id()); assert!(self.req.matches(id.version())); self.set_version_req(VersionReq::exact(id.version())) @@ -303,8 +303,20 @@ impl Dependency { } /// Lock this dependency to depending on the specified package id - pub fn lock_to(self, id: &PackageId) -> Dependency { - self.clone_inner().lock_to(id).into_dependency() + pub fn lock_to(mut self, id: &PackageId) -> Dependency { + Rc::make_mut(&mut self.inner).lock_to(id); + self + } + + /// Set the version requirement for this dependency + pub fn set_version_req(&mut self, req: VersionReq) -> &mut Dependency { + Rc::make_mut(&mut self.inner).set_version_req(req); + self + } + + pub fn set_kind(&mut self, kind: Kind) -> &mut Dependency { + Rc::make_mut(&mut self.inner).set_kind(kind); + self } /// Returns false if the dependency is only used to build the local package. @@ -327,14 +339,14 @@ impl Dependency { self.inner.matches_id(id) } - pub fn map_source(self, to_replace: &SourceId, replace_with: &SourceId) + pub fn map_source(mut self, to_replace: &SourceId, replace_with: &SourceId) -> Dependency { if self.source_id() != to_replace { self } else { - Rc::try_unwrap(self.inner).unwrap_or_else(|r| (*r).clone()) - .set_source_id(replace_with.clone()) - .into_dependency() + Rc::make_mut(&mut self.inner) + .set_source_id(replace_with.clone()); + self } } } diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index 5853774b2..036d23dda 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -152,7 +152,7 @@ impl<'cfg> RegistryIndex<'cfg> { name, req, features, optional, default_features, target, kind } = dep; - let dep = DependencyInner::parse(&name, Some(&req), &self.source_id, None)?; + let mut dep = DependencyInner::parse(&name, Some(&req), &self.source_id, None)?; let kind = match kind.as_ref().map(|s| &s[..]).unwrap_or("") { "dev" => Kind::Development, "build" => Kind::Build, @@ -171,12 +171,12 @@ impl<'cfg> RegistryIndex<'cfg> { // out here. let features = features.into_iter().filter(|s| !s.is_empty()).collect(); - Ok(dep.set_optional(optional) - .set_default_features(default_features) - .set_features(features) - .set_platform(platform) - .set_kind(kind) - .into_dependency()) + dep.set_optional(optional) + .set_default_features(default_features) + .set_features(features) + .set_platform(platform) + .set_kind(kind); + Ok(dep.into_dependency()) } pub fn query(&mut self, diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 02c5e5eae..b665d322d 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -960,17 +960,15 @@ impl TomlManifest { requirement, but found one for `{}`", spec); } - let dep = replacement.to_dependency(spec.name(), cx, None)?; - let dep = { + let mut dep = replacement.to_dependency(spec.name(), cx, None)?; + { let version = spec.version().ok_or_else(|| { CargoError::from(format!("replacements must specify a version \ to replace, but `{}` does not", spec)) })?; - let req = VersionReq::exact(version); - dep.clone_inner().set_version_req(req) - .into_dependency() - }; + dep.set_version_req(VersionReq::exact(version)); + } replace.push((spec, dep)); } Ok(replace) @@ -1118,14 +1116,14 @@ impl TomlDependency { } None => DependencyInner::parse(name, version, &new_source_id, None)?, }; - dep = dep.set_features(details.features.unwrap_or(Vec::new())) - .set_default_features(details.default_features - .or(details.default_features2) - .unwrap_or(true)) - .set_optional(details.optional.unwrap_or(false)) - .set_platform(cx.platform.clone()); + dep.set_features(details.features.unwrap_or(Vec::new())) + .set_default_features(details.default_features + .or(details.default_features2) + .unwrap_or(true)) + .set_optional(details.optional.unwrap_or(false)) + .set_platform(cx.platform.clone()); if let Some(kind) = kind { - dep = dep.set_kind(kind); + dep.set_kind(kind); } Ok(dep.into_dependency()) } diff --git a/tests/resolve.rs b/tests/resolve.rs index e0d19c4b1..47b1e585b 100644 --- a/tests/resolve.rs +++ b/tests/resolve.rs @@ -109,7 +109,7 @@ fn dep_loc(name: &str, location: &str) -> Dependency { Dependency::parse_no_deprecated(name, Some("1.0.0"), &source_id).unwrap() } fn dep_kind(name: &str, kind: Kind) -> Dependency { - dep(name).clone_inner().set_kind(kind).into_dependency() + dep(name).set_kind(kind).clone() } fn registry(pkgs: Vec) -> Vec { -- 2.30.2